package com.gu.job;


import com.gu.domain.entity.Article;
import com.gu.domain.service.ArticleService;
import com.gu.domain.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class UpdateViewCountJob {


    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ArticleService articleService;


    @Scheduled(cron = "0/5 * * * * ?")
    public void updateViewCount(){
        //更新redis中所有的浏览量
        Map<String, Integer> viewCountMap = redisCache.getCacheMap("article:viewCount");

        List<Article> articles = viewCountMap.entrySet()
                .stream()
                        .map(entry-> new Article(Long.valueOf(entry.getKey()),entry.getValue().longValue()))
                                .collect(Collectors.toList());
        //更新数据库中所有的浏览量
        articleService.updateBatchById(articles);
    }
}
